/*
 *  请求session,对应每个客户端请求,存在一个RequestSession对象,在请求未完成前存在
 */

package com.moon.game.command;

import java.util.HashMap;

import org.jboss.netty.channel.MessageEvent;

import com.moon.game.clazz.PBMessageProto.PBMessage;
import com.moon.game.client.UdpClient;
import com.moon.game.manager.PlayerMG;
import com.moon.game.message.ObjMessage;
import com.moon.game.server.SessionPool;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.PlayerVo;

public class RequestSession {
    private int sessionId = 0; // 系统生成、唯一标识一个请求对应的session
    private int status = 0; // session所处的状态,-1为结束 ; 0为初始
    private ObjMessage clientRequest = null; // 客户端请求对象
    private MessageEvent event = null; // 客户端连接对象
    private String cmd = "UNKNOWN"; // 客户端请求的cmd(clientRequest里的副本)
    private ObjMessage clientResponse = null; // 给客户端的回包
    private HashMap<String, Object> tempDataMap = new HashMap<String, Object>(); // session中的临时存储对象
    private String extLog = null;
    private int createTime = 0;

    private int reSendFlag = 0; // 0-不需要重发,做超时处理 1-需要重发 2-超时忽略
    private PBMessage last_req = null; // last send
    private UdpClient last_udpclient = null;

    // / endof 2011-1-15

    public RequestSession() {
    }

    public RequestSession(int sessionId, SessionPool.UniData unidata) {
        this.sessionId = sessionId;
        this.event = unidata.event;
        this.clientRequest = (ObjMessage) (unidata.data);
        this.clientResponse = new ObjMessage();
        this.clientResponse.setCmd(clientRequest.getCmd());
        this.clientResponse.setPid(clientRequest.getPid());
        this.clientResponse.setSid(clientRequest.getSid());
        cmd = this.clientRequest.getCmd();
        status = 0;
        this.createTime = GDateUtils.now();
    }

    public int getSessionId() {
        return sessionId;
    }

    public void setSessionId(int sessionId) {
        this.sessionId = sessionId;
    }

    public int getStatus() {
        return status;
    }

    public int getStatusPlus() {
        return ++status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getReSendFlag() {
        return reSendFlag;
    }

    public void setReSendFlag(int reSendFlag) {
        this.reSendFlag = reSendFlag;
    }

    public PBMessage getLast_req() {
        return last_req;
    }

    public void setLast_req(PBMessage lastReq) {
        last_req = lastReq;
    }

    public UdpClient getLast_udpclient() {
        return last_udpclient;
    }

    public void setLast_udpclient(UdpClient lastUdpclient) {
        last_udpclient = lastUdpclient;
    }

    public ObjMessage getClientRequest() {
        return clientRequest;
    }

    public void setClientRequest(ObjMessage clientRequest) {
        this.clientRequest = clientRequest;
    }


    public MessageEvent getEvent() {
		return event;
	}

	public void setEvent(MessageEvent event) {
		this.event = event;
	}

	public ObjMessage getClientResponse() {
        return clientResponse;
    }

    public void setClientResponse(ObjMessage clientResponse) {
        this.clientResponse = clientResponse;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    public void setVO(String label, Object vo) {
        clientResponse.getData().put(label, vo);
    }

    public String getExtLog() {
        return extLog;
    }

    public void setExtLog(String extLog) {
        this.extLog = extLog;
    }

    public Object getVO(String label) {
        return clientResponse.getData().get(label);
    }

    public void setTempData(String label, Object vo) {
        tempDataMap.put(label, vo);
    }

    public Object getTempData(String label) {
        return tempDataMap.get(label);
    }

    public Object removeTempData(String label) {
        return tempDataMap.remove(label);
    }

    /**
     * 返回玩家信息
     **/
    public PlayerVo getPlayer() {
    	if (getPlayerId() == null) {
			return null;
		}
        return PlayerMG.instance.getPlayer(getPlayerId());
    }

    public String getPlayerId() {
        return (String) tempDataMap.get(Command.PAR_ID);
    }
    
    public String getSid() {
    	return (String) tempDataMap.get(Command.PAR_SID);
    }

    public int getCreateTime() {
        return createTime;
    }

    public void setCreateTime(int createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "RequestSession [clientRequest=" + clientRequest + ", cmd=" + cmd + ", sessionId=" + sessionId + ", status=" + status + "]";
    }
}
